//颜色分类
//测试链接 https://leetcode.cn/problems/sort-colors/
public class SortColors {
    public void sortColors(int[] nums) {
        int i = 0, left = -1, right = nums.length;

        while(i < right){
            if(nums[i] == 1){
                i++;
            }else if(nums[i] == 0){
                swap(nums,left+1,i);
                i++;
                left++;
            }else{ //nums[i] == 2
                swap(nums, i,right-1);
                right--;
            }
        }
    }

    public void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
